Find All Anagrams in a String
Try to solve the Find All Anagrams in a String problem.
We'll cover the following
Statement#
Given two strings, a and b, return an array of all the start indexes of anagrams of b in a. We may return the answer in any order.
An anagram is a word or phrase created by rearranging the letters of another word or phrase while utilizing each of the original letters exactly once. For example, “act” is the anagram of “cat”, and “flow” is the anagram of “wolf”.
Constraints:
-
a.length,b.length - Both
aandbconsist only of lowercase English letters.
Examples#
1 of 3
2 of 3
3 of 3
Understand the problem#
Let’s take a moment to make sure you’ve correctly understood the problem. The quiz below helps you to check if you’re solving the correct problem:
Find All Anagrams in a String
What are all the anagrams of string b in string a if the following inputs are given?
a = “teaateat”
b = “eat”
“aet” (starts from index 2)
“eta” (starts from index 5)
“tae” (starts from index 7)
We need anagrams only in the forward direction of string a and not in the reverse direction. All the given anagrams are invalid.
“tea” (starts from index 1)
“ate” (starts from index 4)
“tea” (starts from index 5)
“eat” (starts from index 6)
“tea” (starts from index 0)
“ate” (starts from index 3)
“tea” (starts from index 4)
“eat” (starts from index 5)
These are the only four anagrams of string b in string a.
“tea” (starts from index 1)
“tae” (starts from index 7)
Figure it out#
We have a game for you to play. Rearrange the logical building blocks to develop a clearer understanding of how to solve this problem.
Try it yourself#
Implement your solution in main.py in the following coding playground. We've provided a useful code template in the other file that you may build on to solve this problem.
Solution: First Unique Character in a String
Solution: Find All Anagrams in a String